# -*- coding: utf-8 -*-

from lib.MapChart import MapChart
from util.functions import *

class MapGradientChart(MapChart):
      def __init__(self, data, datarange, config = {}):
          self.defaults["gradient-start"] = "#B0C4DE"
          self.defaults["gradient-end"] = "#4682B4"
          self.config = config
          self._fillConfig()
          self.datarange = datarange
          self.gradient = gradient(self.config["gradient-start"], self.config["gradient-end"], len(self.datarange)+1)
          self.rawdata = data
          self._setColors()

      def _setColors(self):
          self.data = {}
          for d in self.rawdata:
              x = self.rawdata[d]
              last = None
              c = 0
              for y in self.datarange:
                  if c == 0 and x>y:
                     self.data[d] = self.gradient[c]
                  if last<x and y>x:
                     self.data[d] = self.gradient[self.datarange.index(last)]
                  if c == len(self.datarange)-1 and x>y:
                     self.data[d] = self.gradient[c]
                  last = y
                  c += 1
          q = 0
          l = "0"
          self.legend = []
          if self.datarange[0] == 1:
             self.datarange = self.datarange[1:]
             l = "1"
          for c in self.datarange:
              self.legend.append([self.gradient[q],l+"-"+str(c)])
              l = str(c)
              q+=1
          self.legend.append([self.gradient[q],l+"+"])

      
